const { BASE_URL, PIC_URL } = require('../../config')
const { replaceBaseUrl } = require('../../utils/util')
// pages/user/user.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 应该持久化到
    features: [
      {
        name: "考试记录",
        url: "/pkg/pages/testRecord/testRecord",
        icon: "iconfont icon-record"
      },
      {
        name: "考试排行",
        url: "/pkg/pages/rankings/rankings",
        icon: "iconfont icon-Ranking"
      },
      {
        name: "我的喜欢",
        url: "/pkg/pages/likes/likes",
        icon: "iconfont icon-thumb"
      },
      {
        name: "积分记录",
        url: "/pkg/pages/pointsRecord/pointsRecord",
        icon: "iconfont icon-money-funds"
      },
      {
        name: "积分兑换",
        url: "/pages/exchange/exchange",
        icon: "iconfont icon-money-rmb-fill"
      },
      {
        name: "积分说明",
        url: "/points-info",
        icon: "iconfont icon-info"
      },
      {
        name: "我的订单",
        url: "/pkg/pages/myOrder/myOrder",
        icon: "iconfont icon-dingdan"
      },
      {
        name: "关于我们",
        url: "/about-us",
        icon: "iconfont icon-aboutshare"
      },
      {
        name: "联系客服",
        url: "/contact-customer-service",
        icon: "iconfont icon-WeChat"
      }
    ],
    avatar: '',
    nickname: '',
    credits: 0
  },
  toIdentifyRecord() {
    wx.navigateTo({
      url: '/modules/user/identifyRecord/index',
    })
  },
  async onSignIn() {
    const token = wx.getStorageSync('token')
    const {data:res} = await wx.p.request({
      url: BASE_URL + '/me/sign_in',
      method: 'POST',
      header: {
        token: token
      }
    })
    if (res.code === 200)
      return wx.showModal({
        title: '提示',
        content: `${res.msg}`
      })
  },
  async _getUserInfo() {
    const token = wx.getStorageSync('token')
    const res = await wx.p.request({
      url: BASE_URL + '/me/user_info',
      header: {
        token: token
      }
    })
    let avatar = replaceBaseUrl(PIC_URL,res.data.data.avatar)
    this.setData({
      avatar: avatar,
      nickname: res.data.data.name,
      credits: res.data.data.credits
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 用户基本信息请求
    this._getUserInfo()

    // const user = wx.getStorageSync('user')
    // this.setData({
    //   avatar: user.avatar,
    //   nickname: user.name
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})